/*
 * Created by JFormDesigner on Wed Nov 20 22:17:47 GMT+08:00 2024
 */

package com.hafu.view.kehu;
import java.awt.event.*;
import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author m1528
 */
public class ManageKeHuFrm extends JFrame {
    private KhInfoService khInfoService = new KhInfoService();
    private int chooseld;
    public ManageKeHuFrm() {
        initComponents();
        fillTable();
    }
    private void fillTable() {
        //
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        //获取所有客户信息
        ArrayList<KhInfo> khInfoList = khInfoService.getKhInfoByName(new KhInfo(0, textField1.getText(), null));
        for (KhInfo khInfo : khInfoList) {
            Object[] rowDate = {khInfo.getId(), khInfo.getName(), khInfo.getTel()};
            dtm.addRow(rowDate);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        String name = textField1.getText();
        KhInfo khInfo = new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = khInfoService.getKhInfoByName(khInfo);
        if (khInfoByName.size() > 0.){
            for (KhInfo khInfo1 : khInfoByName) {
                Object[] rowDate = {khInfo1.getId(), khInfo1.getName(), khInfo1.getTel()};
                dtm.addRow(rowDate);
            }
        }else {
            JOptionPane.showMessageDialog(null, "没有找到客户信息");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        chooseld = (int) dtm.getValueAt(row, 0);
        String name = (String) dtm.getValueAt(row, 1);
        String tel = (String) dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }

    private void button2(ActionEvent e) {
        // TODO add your code here
        String name = textField2.getText();
        String tel = textField3.getText();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int row = table1.getSelectedRow();
        int id = (int) dtm.getValueAt(row, 0);
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "客户姓名不能为空！");
        }
        if (StringUtil.isEmpty(tel)) {
            JOptionPane.showMessageDialog(null, "客户电话不能为空！");
        }
        KhInfo khInfo = new KhInfo(id, name, tel);
        Boolean updateKhInfo = khInfoService.updateKhInfo(khInfo);
        if (updateKhInfo) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean deleteKhInfo = khInfoService.deleteKhInfo(id);
        if (deleteKhInfo) {
            JOptionPane.showMessageDialog(null, "删除成功！");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "删除失败！");
        }
    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5ba2\u6237\u8868\u5355");
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        //---- label1 ----
        label1.setText("\u5ba2\u6237\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(this::button1);

        //---- label2 ----
        label2.setText("\u5ba2\u6237\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u5ba2\u6237\u7535\u8bdd");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(11, 11, 11)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(label2)
                                .addComponent(label3))
                            .addGap(32, 32, 32)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                            .addGap(92, 92, 92)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(button2, GroupLayout.Alignment.TRAILING)
                                .addComponent(button3, GroupLayout.Alignment.TRAILING)))
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 440, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1)
                            .addGap(37, 37, 37)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(82, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1)
                        .addComponent(button1))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2)
                        .addComponent(button2))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3)
                        .addComponent(button3)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(12, 12, 12))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
